BottomButtons = {
  showing: "map"

  slider_panToActualPositionChanged: (e) ->
    $slider = $(e.target)
    if $slider.val() is "yes"
      Map.panToActualPosition = true
    if $slider.val() is "no"
      Map.panToActualPosition = false

  handleFriendsPost: (data) ->
    if data
      Geo.raiseNotice "(de momento) es probable que tengas que refrescar la página para ver tus nuevos amigos. <a href='/'>Refrescar</a>", "notice"
    else
      console.error "hubo un error enviando la solicitud al servidor"

  friendsChanged: (e) ->
    $slider = $(e.target)
    if $slider.val() is "yes"
      $.post "/friendship/" + $slider.data("person_id"), BottomButtons.handleFriendsPost
    if $slider.val() is "no"
      $.post "/friendship/" + $slider.data("person_id") + "/destroy", BottomButtons.handleFriendsPost

  handleScrolling: (e) ->
    if BottomButtons.showing is "map"
      e.preventDefault()

  clicked_right: ->
    BottomButtons.showing = "preferences"
    $("#main_content_left").hide()
    $("#main_content_right").show()

  clicked_left: ->
    BottomButtons.showing = "map"
    $("#main_content_right").hide()
    $("#main_content_left").show()
    window.scrollTo 0, 1

  init: ->
    $("#preferencesButton").live "click", BottomButtons.clicked_right
    $("#mapButton").live "click", BottomButtons.clicked_left
    $(".friendsToggleSlider").live "change", BottomButtons.friendsChanged
    $("#slider_panToActualPosition").live "change", BottomButtons.slider_panToActualPositionChanged
}

# Handle scrolling:
$(document).live "touchmove", BottomButtons.handleScrolling

jQuery ->
  if document.getElementById('authenticated')
    BottomButtons.init()